package com.jielihaofeng.site.tag.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import com.jielihaofeng.site.tag.convert.TagConvert;
import com.jielihaofeng.site.tag.entity.TagEntity;
import com.jielihaofeng.site.tag.query.TagQuery;
import com.jielihaofeng.site.tag.service.TagService;
import com.jielihaofeng.site.tag.vo.TagVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 标签管理
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-21
 */
@RestController
@RequestMapping("site/tag")
@Tag(name = "标签管理")
@AllArgsConstructor
public class TagController {
    private final TagService tagService;

    @GetMapping("list")
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('site:tag:list')")
    public Result<List<TagVO>> list() {
        List<TagEntity> list = tagService.list();
        return Result.ok(TagConvert.INSTANCE.convertList(list));
    }

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('site:tag:page')")
    public Result<PageResult<TagVO>> page(@Valid TagQuery query) {
        PageResult<TagVO> page = tagService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('site:tag:info')")
    public Result<TagVO> get(@PathVariable("id") Long id) {
        TagEntity entity = tagService.getById(id);

        return Result.ok(TagConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('site:tag:save')")
    public Result<String> save(@RequestBody TagVO vo) {
        tagService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('site:tag:update')")
    public Result<String> update(@RequestBody @Valid TagVO vo) {
        tagService.update(vo);

        return Result.ok();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('site:tag:delete')")
    public Result<String> delete(@PathVariable("id") Long id){
        tagService.delete(id);

        return Result.ok();
    }
}